package scau.mis4.chanyulin.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import scau.mis4.chanyulin.base.common.BaseController;
import scau.mis4.chanyulin.entity.*;
import scau.mis4.chanyulin.service.IGoodDetailService;
import scau.mis4.chanyulin.service.IGoodService;
import scau.mis4.chanyulin.service.IOrderItemService;
import scau.mis4.chanyulin.service.IOrderTotService;
import java.util.Date;
import java.util.List;

/**
 * Created by cyl on 2018/03/27.
 * OrderTot Controller层
 */
@RestController
@RequestMapping("/ordertot")
public class OrderTotController extends BaseController {

    @Autowired
    private IOrderTotService orderTotService;
    @Autowired
    private IOrderItemService orderItemService;
    @Autowired
    private IGoodDetailService goodDetailService;
    @Autowired
    private IGoodService goodService;

    /**
     * 添加Order
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @RequiresRoles(value={"0","1", "2"},logical = Logical.OR)
    public String addOrderTot(@RequestBody JSONObject jsonObject) throws Exception {

        // 从json获取总订单
        OrderTot order = jsonObject.getObject("order", OrderTot.class);
        order.setCreateTime(new Date());
        // 从json获取订单详情列表
        JSONArray orderDetailArrays = jsonObject.getJSONArray("orderDetailList");

        // 添加总订单
        orderTotService.add(order);
        // 遍历每个子订单
        for (int i = 0; i < orderDetailArrays.size(); i++) {

            JSONObject obj = orderDetailArrays.getJSONObject(i);

            double receipt = obj.getDouble("receipt");
            String detailId = obj.getString("detailId");
            int num = obj.getInteger("num");
            String promotionId = obj.getString("promotionId");

            GoodDetail goodDetail = goodDetailService.findByKey(detailId);
            Good good = goodService.findByKey(goodDetail.getGoodId());
            OrderItem orderItem = new OrderItem(order.getId(), goodDetail.getId(), receipt,
                    goodDetail.getReferencePrice(), good.getTitle(), good.getClazz().getName(),
                    good.getBrand().getName(), goodDetail.getColor(), goodDetail.getSize(),
                    goodDetail.getGender(), goodDetail.getAge(), "已完成", num, promotionId
            );

            // 添加子订单
            orderItemService.add(orderItem);
        }

        return createSuccessJSON();
    }

    /**
     * 删除某个Order
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @RequiresRoles(value={"0","1", "2"},logical = Logical.OR)
    public String deleteOrderById(String id) throws Exception {
        orderTotService.delete(id);
        return createSuccessJSON();
    }

    /**
     * 查找某个Order
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/get")
    public String getOrderById(String id) throws Exception {
        OrderTot order = orderTotService.findByKey(id);
        Model model = new ExtendedModelMap().addAttribute("result", order);
        return createSuccessJSON(model);
    }

    /**
     * 获取全部Order
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public String listAllOrder() throws Exception {
        List<OrderTot> orderList = orderTotService.findAll();
        Model model = new ExtendedModelMap().addAttribute("result",orderList);
        return createSuccessJSON(model);
    }

    /**
     * 分页查询Order
     * @param pageNum 页码
     * @param pageSize 每页数据数量
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public String listOrderByPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderTot> orderList = orderTotService.findAll();
        PageInfo<OrderTot> pageInfo = new PageInfo<>(orderList);
        Model model = new ExtendedModelMap()
                .addAttribute("result", orderList)
                .addAttribute("pageInfo", pageInfo);
        return createSuccessJSON(model);
    }

    /**
     * 更新某个Order
     * @param order
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @RequiresRoles(value={"0","1", "2"},logical = Logical.OR)
    public String updateOrderById(OrderTot order) throws Exception {
        orderTotService.updateById(order);
        return createSuccessJSON();
    }

}
